<?php
$route = [
    '*/user/settings',
    '/user/level',
    '/content.help/list',
    '/content.help/detail',
    '/content.help/add'
];
$method = [
    '*' => 'POST',
    '^' => 'GET'
];
$structure = [];
$methodKeys = array_keys($method);
foreach ($route as $key => $item) {
    $apiMehotd = $method['^'];
    if (in_array($item[0], $methodKeys)) {
        $apiMehotd = $method[$item[0]];
        $item = substr($item, 1, strlen($item));
    }
    $v = preg_match_all('/(\w+)/', $item, $pool);
    $structure[] = [
        'path' => $item,
        'key' => implode('_', $pool[0]) . '_' . mt_rand(100000, 999999),
        'method' => $apiMehotd
    ];
}
$html = <<<TEXT
/**
 * api接口列表
 */
const api = {};\n
TEXT;

foreach ($structure as $key => $item) {
    $html .= "\napi['url_{$item['key']}']=\"{$item['path']}\";";
}
$html .= <<<TEXT
\n
TEXT;
foreach ($structure as $key => $item) {
    $html.="\n/**
* @url {$item['path']}
* @param {*} data
* @method {$item['method']}
*/
    ";
    $html .= "export function fetch_{$item['key']}(data){
        return request({
            url:api.url_{$item['key']},
            method:\"{$item['method']}\",
            data:data
        })
    };\n";
}
echo $html;
file_put_contents('fetch.js', $html);